/*!
 * Copyright (c) 2023-present StarDreamNet
 */
(document.loader || (document.loader = [])).push(["postlist", class {
    constructor() { };

    loadPostsByData(data, $el) {
        var cards = $el;
        for (let d of data) {
            var card = $(`<div class="app-cards-item"></div>`);

            var user = $(`<div class="app-cards-item-user"></div>`);
            var userImg = $(`<img class="app-cards-item-user-img" width="47" height="47">`);
            var userInfo = $(`<div class="app-cards-item-user-info"></div>`);
            var userInfoName = this.renderNameAndGroups(d.user_name, d.user_group || [], "/user/" + d.user_id);
            var userInfoDate = $(`<div class="app-cards-item-user-info-date"></div>`);

            var titleUp = $('<div class="app-post-title-up">置</div>');
            var titleBest = $('<div class="app-post-title-best">精</div>');
            var titleHot = $('<div class="app-post-title-hot">热</div>');

            var title = $(`<a class="app-cards-item-title app-post-title"></a>`);
            var content = $(`<div class="app-cards-item-content"></div>`);
            var img = $(`<img class="app-cards-item-img" width="200">`);
            var test = $("<div></div>");

            test.html(editormd.filterHTMLTags(marked(d.text, { sanitize: true }), false).replace(/<img[^>]*>/g, ''));

            userInfo.append(userInfoName, userInfoDate);
            user.append(userImg, userInfo);
            card.append(user, title, content);
            if (d.img !== "") card.append(img);
            cards.append(card);

            userImg.attr("src", d.user_img);
            userImg.attr("alt", d.user_name);
            userInfoDate.text(this.dateSpanString(d.time * 1000, Date.now()));

            title.text(d.title);
            title.attr("href", "/post/" + d.id);

            if (d.ison) title.append(titleUp);
            if (d.isbest) title.append(titleBest);
            if (d.ishot) title.append(titleHot);

            content.text(test.text());

            img.attr("src", d.img);
            img.attr("alt", d.title);
        }
    };

    init_(require) {
        const { dateSpanString, renderNameAndGroups } = require("index");

        this.dateSpanString = dateSpanString;
        this.renderNameAndGroups = renderNameAndGroups;
    };
}])